﻿@using TaskoMask.BuildingBlocks.Contracts.Dtos.Cards
@using TaskoMask.BuildingBlocks.Contracts.Enums
@inject CardApiService CardApiService

<EditForm Model="CardModel" OnValidSubmit="HandleUpdateCard">

    <DataAnnotationsValidator />

    <input type="hidden" @bind="CardModel.BoardId" />


    <div class="form-group">
        <InputText @bind-Value="CardModel.Name" placeholder="Name" class="form-control"></InputText>
        <ValidationMessage For="()=>CardModel.Name" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <InputSelect @bind-Value="CardModel.Type" class="form-control">
            @foreach (var type in Enum.GetValues(typeof(BoardCardType)))
            {
                <option value="@type">@type</option>
            }
        </InputSelect>
        <ValidationMessage For="()=>CardModel.Type" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary float-left">Update</button>
        <button @onclick="HandleDelete" class="btn btn-danger text-white float-right">Delete</button>
    </div>

</EditForm>


@code {

    #region Fields and Parameters


    [Parameter]
    public string CardId { get; set; }

    UpdateCardDto CardModel = new UpdateCardDto();

    /// <summary>
    /// Access to current modal
    /// </summary>
    [CascadingParameter]
    BlazoredModalInstance ModalInstance { get; set; }



    #endregion Fields and Parameters



    #region Protected Methods


    /// <summary>
    ///
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        await PrepareModelToUpdate();

    }


    #endregion Protected Methods


    #region private Methods



    /// <summary>
    /// Get model by Id from API
    /// </summary>
    private async Task PrepareModelToUpdate()
    {

        var cardResult = await CardApiService.GetAsync(CardId);

        if (cardResult.IsSuccess)
        {
            CardModel = new UpdateCardDto
                {
                    Id = cardResult.Value.Id,
                    Name = cardResult.Value.Name,
                    Type = cardResult.Value.Type,
                    BoardId = cardResult.Value.BoardId,
                };
        }
        else
        {
            QueryResultHandler<GetCardDto>.Init()
                .WithResult(cardResult)
                .ShowErrorToast(ToastService)
                .CloseModal(ModalInstance);
        }
    }



    /// <summary>
    /// handle update model
    /// </summary>
    private async Task HandleUpdateCard()
    {
        var result = await CardApiService.UpdateAsync(CardId, CardModel);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Card_Updated)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService)
            .CloseModal(ModalInstance);
    }



    /// <summary>
    /// handle delete item
    /// </summary>
    private async Task HandleDelete()
    {
        var confirmationModal = ModalService.Show<Confirmation>("Deletion Warning!", new ModalOptions { Class = "blazored-modal w-75 w-md-50 w-md-50 bg-light-gray" });
        var confirmationModalResult = await confirmationModal.Result;

        if (confirmationModalResult.Cancelled)
            return;

        var result = await CardApiService.DeleteAsync(CardId);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Card_Deleted)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService)
            .CloseModal(ModalInstance);
    }

    #endregion private Methods

}
